#include "Program.h"

using namespace skelepp::Core::Application;
using namespace skelepp::Core::Messaging;

int main(int argc, char *argv[]) {
	Program::get()->start();

	while (Program::get()->is_running()) {
		yield();
	}

	return 0;
}

Program *Program::_instance = NULL;

Program::Program(void) {
	_running = true;
	
	_settings = new Settings();
	_thread_pool = new ThreadPool(8);
	_broker = new Broker();
}

Program::~Program(void) {
	delete _settings;
	delete _thread_pool;
	delete _broker;
}

Program *Program::get(void) {
	static Cleanup _cleanup;

	// lazy loading
	if (!_instance) {
		_instance = new Program();
	}

	return _instance;
}

Program::Cleanup::~Cleanup(void) {
	if (_instance) {
		delete _instance;
		_instance = NULL;
	}
}

void Program::start(void) {
	_running = true;
	/*for (uint32_t i = 1; i <= 100; i++) {
		Cout *c = new Cout(i, false);
		Program::get()->get_thread_pool()->perform(c);
	}
	for (uint32_t i = 101; i <= 150; i++) {
		Cout *c = new Cout(i, true);
		Program::get()->get_thread_pool()->perform(c);
	}*/
}

void Program::stop(void) {
	_running = false;
}

bool Program::is_running(void) {
	return _running;
}